package org.bigdata.zc.mongo.repository;

import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import org.bigdata.zc.CaculateModel;
import org.bigdata.zc.GlobalValFactory;
import org.bigdata.zc.config.GlobalVal;
import org.bigdata.zc.mongo.CupWin;
import org.bigdata.zc.mongo.MatchScheduleWin;
import org.bigdata.zc.mongo.TeamWin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Stephen on 2017/10/31.
 */
@Repository
public class MatchScheduleRpt {

    @Resource
    MongoTemplate template;

    @Autowired
    TeamRepository teamRepository;

    @Autowired
    CupRepository cupRepository;

    @Autowired
    GlobalVal globalVal;

    public List<MatchScheduleWin> findMatchScheduleWinByDateTime(String cupId, Date startDate, Date endDate) {

//        Criteria.where("startDate").gte(startDate)
//                .and("startDate").lt(endDate)
//        Criteria criteria1 = Criteria.where("createTime").gte("matchTime").and("matchTime").lte(endDate);
//        Criteria criteria =new Criteria().andOperator(Criteria.where("matchTime").gte(startDate),Criteria.where("matchTime").lt(endDate));
//       // = Criteria.where("matchTime").lt("ISODate('2017-10-01T00:00:00Z')");
//        query.addCriteria(criteria);

        //query.put("dateAdded", BasicDBObjectBuilder.start("$gte", startDate).add("$lte", endDate).get());
        Criteria criteria = new Criteria().andOperator(Criteria.where("matchTime").gt(startDate).lte(endDate), Criteria.where("cupId").is(cupId));
//        Query query = new Query().addCriteria(Criteria.where("matchTime").gt(startDate).lte(endDate),Criteria.where("cupId").lt(endDate));
        Query query = new Query(criteria);
        List<MatchScheduleWin> matchScheduleWins = template.find(query, MatchScheduleWin.class);
        matchScheduleWins = getMatchScheduleResult(cupId,matchScheduleWins);
        //设置预测算法
        return matchScheduleWins;
    }



    public List<MatchScheduleWin> getMatchScheduleResult(String cupId, List<MatchScheduleWin> matchScheduleWins) {
        List<MatchScheduleWin> result = new ArrayList<>(matchScheduleWins.size());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        double allCount = 0;
        double winCount = 0;
        double blankCount = 0;
        double allCount2 = 0;
        double winCount2 = 0;
        double blankCount2 = 0;
        Double x = 0.0;
        for (MatchScheduleWin matchScheduleWin : matchScheduleWins) {
            CaculateModel caculateModel = caulateSomeMatchMap(format.format(matchScheduleWin.getMatchTime()), matchScheduleWin);
            if(null !=caculateModel&&null!=caculateModel.getValue()){
                if(caculateModel.getValue()<= GlobalValFactory.getValue1() ||caculateModel.getValue()>=GlobalValFactory.getValue2()){
                    caculateModel.setHasContains(true);
                }
            }
            matchScheduleWin.setCaculateModel(caculateModel);
            result.add(matchScheduleWin);
        }
        return result;
    }
    public CaculateModel caulateSomeMatchMap(String date, MatchScheduleWin matchScheduleWin) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        CaculateModel caculateModel = new CaculateModel();
        Double result = 0.0;
        int homeId = matchScheduleWin.getHomeId();
        int vistorId = matchScheduleWin.getVisitingId();
        List<TeamWin> teamWin = teamRepository.findByTeamId(homeId + "");
        List<TeamWin> vistorWin = teamRepository.findByTeamId(vistorId + "");
        if (CollectionUtils.isEmpty(teamWin) || CollectionUtils.isEmpty(vistorWin)) {
            return null;
        }
        TeamWin win1 = teamWin.get(0);
        TeamWin win2 = vistorWin.get(0);
        if (null != win1.getAllPrice() && null != win2.getAllPrice()) {
            double price1 = Double.valueOf(win1.getAllPrice());
            double price2 = Double.valueOf(win2.getAllPrice());
            result = win1.getTeamDataWin().getWinChance() / 100 * price1;
            Double result2 = win2.getTeamDataWin().getWinChance() / 100 * price2;
            //System.out.println(date+"主队："+win1.getTeamName()+":客队"+win2.getTeamName()+"比分"+matchScheduleWin.getHomeScore()+":"+matchScheduleWin.getVisitingScore()+"预测的概率"+"_"+(price1-price2));
            caculateModel.setDate(date);
            caculateModel.setHostName(win1.getTeamName());
            caculateModel.setVistorName(win2.getTeamName());
            caculateModel.setScore(matchScheduleWin.getHomeScore() + ":" + matchScheduleWin.getVisitingScore());
            caculateModel.setValue(price1 / price2);
            caculateModel.setValue2(price2 / price1);
            caculateModel.setHomeScore(matchScheduleWin.getHomeScore());
            caculateModel.setVisitorScore(matchScheduleWin.getVisitingScore());
            CupWin cupWin = cupRepository.findByCupId(matchScheduleWin.getCupId());
            caculateModel.setCupName(cupWin.getCupName());
            // System.out.println(cupWin.getCupName()+":"+format.format(matchScheduleWin.getMatchTime())+"主队："+win1.getTeamName()+":客队"+win2.getTeamName()+"比分"+matchScheduleWin.getHomeScore()+":"+matchScheduleWin.getVisitingScore()+"预测的概率"+"_"+(price1/price2));
            return caculateModel;
        }

        return null;
    }
    public List<MatchScheduleWin> getTopMatchScheduleResultWithMap(String cupId, List<MatchScheduleWin> matchScheduleWins) {
        List<MatchScheduleWin> result = new ArrayList<>(matchScheduleWins.size());
        Map<String, List<MatchScheduleWin>> dateMatchMaps = new HashMap<>();
        double allCount = 0;
        double winCount = 0;
        double blankCount = 0;
        double allCount2 = 0;
        double winCount2 = 0;
        double blankCount2 = 0;
        for (String key : dateMatchMaps.keySet()) {
            List<MatchScheduleWin> tempMatchSchecheWinList = dateMatchMaps.get(key);
            Double x = 0.0;
            CaculateModel caculateModel2 = new CaculateModel();//此处记录top值
            for (MatchScheduleWin matchScheduleWin : tempMatchSchecheWinList) {
                CaculateModel caculateModel = caulateSomeMatchMap(key, matchScheduleWin);

                if (null != caculateModel) {
                    if (caculateModel.getValue() > x) {
                        x = caculateModel.getValue();
                        caculateModel2 = caculateModel;
                        //System.out.println(caculateModel2.getCupName()+"============"+caculateModel2.getDate()+"主队："+caculateModel2.getHostName()+"客队："+caculateModel2.getVistorName()+"预测值"+caculateModel2.getValue());
                    }
                    if (caculateModel.getValue2() > x) {
                        x = caculateModel.getValue2();
                        caculateModel2 = caculateModel;
                    }
//
                    if (-1 != matchScheduleWin.getHomeScore()) {
                        //     if(caculateModel2.getValue()>0.2&&caculateModel2.getValue()<2.0){
                        if (caculateModel2.getValue() < 0.2 || caculateModel2.getValue() > 2.0) {
                            double reulst = matchScheduleWin.getHomeScore() - matchScheduleWin.getVisitingScore();
                            if (caculateModel.getValue() > 1 && reulst > 0) {
                                winCount++;
                            }
                            if (caculateModel.getValue() < 1 && reulst < 0) {
                                winCount++;
                            }
                            allCount++;
                            System.out.println(caculateModel2.getCupName() + "============" + caculateModel2.getDate() + "主队：" + caculateModel2.getHostName() + "客队：" + caculateModel2.getVistorName() + "比分" + caculateModel2.getScore() + "预测值" + caculateModel2.getValue());
                        } else {

                        }

                    }

                }
            }
            //获取每天一条的内容
            if (null != caculateModel2.getDate()) {
                if (-1 != caculateModel2.getHomeScore()) {
                    double reulst2 = caculateModel2.getHomeScore() - caculateModel2.getVisitorScore();

                    if (caculateModel2.getValue() > 0.2 && caculateModel2.getValue() < 2.0) {
//                        System.out.println(caculateModel2.getCupName()+"============"+caculateModel2.getDate()+"主队："+caculateModel2.getHostName()+"客队："+caculateModel2.getVistorName()+"比分"+caculateModel2.getScore()+"预测值"+caculateModel2.getValue());
                        blankCount2++;
                    } else {
                        if (caculateModel2.getValue() > 1 && reulst2 > 0) {
                            winCount2++;
                        }
                        if (caculateModel2.getValue() < 1 && reulst2 < 0) {
                            winCount2++;
                        }
                        allCount2++;
                        // System.out.println(caculateModel2.getCupName()+"============"+caculateModel2.getDate()+"主队："+caculateModel2.getHostName()+"客队："+caculateModel2.getVistorName()+"比分"+caculateModel2.getScore()+"预测值"+caculateModel2.getValue());
                    }

                }

                //System.out.println("============"+caculateModel2.getDate()+"主队："+caculateModel2.getHostName()+"客队："+caculateModel2.getVistorName()+"预测值"+caculateModel2.getValue());
            }

        }

        return result;
    }


    public List<MatchScheduleWin> findDayOfMatchs(Date startDate, Date endDate) {

//        Criteria.where("startDate").gte(startDate)
//                .and("startDate").lt(endDate)
//        Criteria criteria1 = Criteria.where("createTime").gte("matchTime").and("matchTime").lte(endDate);
//        Criteria criteria =new Criteria().andOperator(Criteria.where("matchTime").gte(startDate),Criteria.where("matchTime").lt(endDate));
//       // = Criteria.where("matchTime").lt("ISODate('2017-10-01T00:00:00Z')");
//        query.addCriteria(criteria);

        //query.put("dateAdded", BasicDBObjectBuilder.start("$gte", startDate).add("$lte", endDate).get());
        Criteria criteria = new Criteria().andOperator(Criteria.where("matchTime").gt(startDate).lte(endDate));
//        Query query = new Query().addCriteria(Criteria.where("matchTime").gt(startDate).lte(endDate),Criteria.where("cupId").lt(endDate));
        Query query = new Query(criteria);
        List<MatchScheduleWin> matchScheduleWins = template.find(query, MatchScheduleWin.class);
        return matchScheduleWins;
    }

    public List<MatchScheduleWin> findUnSyncMatchSchedule() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        Date endDate = calendar.getTime();
        Criteria criteria = new Criteria().andOperator(Criteria.where("homeScore").is(-1).and("matchTime").lte(endDate));
//        Query query = new Query().addCriteria(Criteria.where("matchTime").gt(startDate).lte(endDate),Criteria.where("cupId").lt(endDate));
        Query query = new Query(criteria);
        List<MatchScheduleWin> matchScheduleWins = template.find(query, MatchScheduleWin.class);
        return matchScheduleWins;
    }
}
